package com.tandil.seo;

import android.text.method.DigitsKeyListener;
import utils.Constantes;
import utils.PersistenceData;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.TextKeyListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Configuration extends Activity {

	private LinearLayout patente1_TextViex, patente2_TextViex, patente3_TextViex, patente4_TextViex;
	private TextView pat1, pat2, pat3, pat4;
	private Button actualizarCredito;
	public String[] patentes = {"", "", "", ""};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_configuration);
		bindViews();
		ActualizarTxtPatentes();
		
	}

	private void ActualizarTxtPatentes() {
		patentes = PersistenceData.readPatentes(Configuration.this, Constantes.ARCHIVO_PATENTES);
		pat1.setText(patentes[0]);
		pat2.setText(patentes[1]);
		pat3.setText(patentes[2]);
		pat4.setText(patentes[3]);
	}

	private void bindViews() {
		pat1 = (TextView) findViewById(R.id.pat1);
		pat2 = (TextView) findViewById(R.id.pat2);
		pat3 = (TextView) findViewById(R.id.pat3);
		pat4 = (TextView) findViewById(R.id.pat4);
		
		actualizarCredito = (Button)findViewById(R.id.actualizarSaldo_btn);
		actualizarCredito.setOnClickListener(new ActualizarCreditoOnClickListener());
		
		patente1_TextViex = (LinearLayout)findViewById(R.id.patente1_input);
		patente1_TextViex.setOnClickListener(new Patente1OnclickListener());
		
		patente2_TextViex = (LinearLayout)findViewById(R.id.patente2_input);
		patente2_TextViex.setOnClickListener(new Patente2OnclickListener());
		
		patente3_TextViex = (LinearLayout)findViewById(R.id.patente3_input);
		patente3_TextViex.setOnClickListener(new Patente3OnclickListener());
		
		patente4_TextViex = (LinearLayout)findViewById(R.id.patente4_input);
		patente4_TextViex.setOnClickListener(new Patente4OnclickListener());

	}
	
	public void DialogoIngresoPatente(int indice_patente){
		final int indice = indice_patente - 1 ;
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle(R.string.ingresar_patente);
		alert.setMessage("Ingrese Patente N° " + String.valueOf(indice_patente) + " :");

		// Set an EditText view to get user input 
		final EditText input = new EditText(this);
		input.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.CHARACTERS, true));
		input.setText(patentes[indice]);
		alert.setView(input);
        input.requestFocus();

		alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
		  String value = input.getText().toString();
		  if (patenteIngresadaValida(value)){
			  patentes[indice] = value;
			  PersistenceData.writePatentes(Configuration.this, patentes, Constantes.ARCHIVO_PATENTES);
			  ActualizarTxtPatentes();			  
		  }else 
				Toast.makeText(Configuration.this, R.string.ingrese_patente_con_el_formato_abc123, Toast.LENGTH_SHORT).show();
		}
		});

		alert.setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int whichButton) {}
		});

		alert.show();
	}
	
	private boolean patenteIngresadaValida(String value) {
		if (value.length() == 6) {
			String Char, Num;
			Char = value.substring(0, 3);
			Num = value.substring(3, 6);
			for (int i = 0; i<Char.length(); i++){
				char c = Char.charAt(i);
				if (!Character.isLetter(c)) return false;
			}
			for (int i = 0; i<Num.length(); i++){
				char c = Num.charAt(i);
				if (!Character.isDigit(c)) return false;
			}
			return true;	
		}else 
			return false;			
	}	
	
	public class Patente1OnclickListener implements OnClickListener {
		@Override
		public void onClick(View v) { DialogoIngresoPatente(1);}
	}
	
	public class Patente2OnclickListener implements OnClickListener {
		@Override
		public void onClick(View v) { DialogoIngresoPatente(2);}
	}
	
	public class Patente3OnclickListener implements OnClickListener {
		@Override
		public void onClick(View v) { DialogoIngresoPatente(3);}
	}
	
	public class Patente4OnclickListener implements OnClickListener {
		@Override
		public void onClick(View v) { DialogoIngresoPatente(4);}
	}
	
	public class ActualizarCreditoOnClickListener implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			AlertDialog.Builder alert = new AlertDialog.Builder(Configuration.this);

			alert.setTitle(R.string.actuzlizar_credito);
			alert.setMessage(R.string.ingrese_credito_);

			// Set an EditText view to get user input 
			final EditText input = new EditText(Configuration.this);
            input.setKeyListener(new DigitsKeyListener());
			alert.setView(input);
            input.requestFocus();

			alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				String value = input.getText().toString();
				PersistenceData.writeSaldo(Configuration.this, value, Constantes.ARCHIVO_SALDO);
			}
			});

			alert.setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
			  public void onClick(DialogInterface dialog, int whichButton) {}
			});

			alert.show();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activityconfiguration, menu);
		return true;
	}

}
